const wx2my = require('../../wx2my');

const app = getApp();
Page({
  data: {
    onOff: 0,
    typeid: [],
    trade_id: '',
    macno: '',
    list: [],
    checked: false,
    typeList: [],
    type: '1',
    lispic: [],
    lispic_path: [],
    onOff: 0,
    imgshow: false
  },
  onLoad: function (e) {
    console.log(e);
    var that = this;

    if (e.id) {
      that.setData({
        trade_id: e.id,
        macno: e.macno
      });
    } else {
      that.setData({
        trade_id: '',
        macno: e.macno
      });
    }
  },
  onReady: function () {
    let that = this;
    that.setData({
      lispic_path: []
    });
    that.get_problemList();
    that.get_problemFeed();
    ;
  },
  onShow: function () {},
  toChange: function (e) {
    var that = this;
    var arr = this.data.list;
    var index = e.currentTarget.dataset.id;
    arr[index].checked = !arr[index].checked;
    this.setData({
      list: arr
    });
  },

  //获取常见问题
  get_problemList() {
    var that = this;
    app.wxRequest('problem', {}, function (res) {
      console.log(res);

      if (res.data.code == 1) {
        that.setData({
          list: res.data.data
        });
      } else {
        app.toast(res.data.msg);
      }
    });
  },

  //获取故障类型
  get_problemFeed() {
    var that = this;
    app.wxRequest('feed_list', {}, function (res) {
      console.log(res);

      if (res.data.code == 1) {
        that.setData({
          typeList: res.data.data
        });
      } else {
        app.toast(res.data.msg);
      }
    });
  },

  formSubmit(e) {
    console.log(e);
    if (this.data.onOff == 1) return false;
    let that = this;
    let msg = e.detail.value;
    let cause = msg.cause;
    let name = msg.name;
    let phone = msg.phone;
    that.setData({
      cause: cause,
      name: name,
      phone: phone
    });
    that.setData({
      onOff: 1
    }); // app.loading('提交中..');

    let data = {
      'token': wx2my.getStorageSync('token'),
      'macno': msg.macno,
      'indexes': that.data.typeid,
      'cause': msg.cause,
      'name': msg.name,
      'phone': msg.phone
    };
    console.log(that.data.lispic_path.length);

    if (that.data.lispic_path.length > 0) {
      data.img = that.data.lispic_path.join(',');
    }

    ;
    app.wxRequest('feedback', data, function (res) {
      wx2my.hideLoading();
      console.log(res);

      if (res.data.code == 1) {
        app.toast('感谢您的反馈！');
        setTimeout(function () {
          wx2my.navigateBack();
        }, 2000);
      } else {
        that.setData({
          onOff: 0
        });
        app.toast(res.data.msg);
      }
    });
  },

  scanner() {
    let that = this;
    wx2my.scanCode({
      success: res => {
        console.log(res);
        var res = res.result;

        if (res.indexOf('macno') < 0) {
          wx2my.showToast({
            icon: 'none',
            title: '二维码无效'
          });
        } else {
          that.setData({
            macno: app.fm.getUrlParam('macno', res)
          });
        }
      }
    });
  },

  checkboxChange(e) {
    console.log(e.detail.value);
    this.setData({
      typeid: e.detail.value
    });
    console.log(this.data.typeid);
  },

  choice(e) {
    //上传图片
    let that = this;
    console.log(e)
    app.uploadFile(1, function (flag, idx, msg, data) {
      console.log(msg)
      if (flag == 1) {
        let res = msg.tempFilePaths;
        that.setData({
          indexpic: res[0]
        });

        if (that.data.lispic.length < 3) {
          that.setData({
            lispic: that.data.lispic.concat(res)
          });

          if (that.data.lispic.length == 3) {
            that.setData({
              imgshow: true
            });
          }
        } else {
          wx2my.showModal({
            title: '提示',
            content: '最多只能上传3张图片...'
          });
          return false;
        }
      } else if (flag == 2) {
        if (that.data.lispic_path.length < 3) {
          that.data.lispic_path.push(msg.filename);
        }

        console.log(that.data.lispic_path);
      }
    }, {
      'api_name': 'uploadImg',
      'type': 1,
      'token': app.globalData.token
    }, '', 3 - that.data.lispic_path.length);
  },

  previewImage: function (e) {
    wx2my.previewImage({
      current: e.target.dataset.url,
      // 当前显示图片的http链接
      urls: [e.target.dataset.url],
      // 需要预览的图片http链接列表
      success: function (res) {
        console.log(res);
      }
    });
  },

  delete(e) {
    let lispic = this.data.lispic;
    let lispic_path = this.data.lispic_path;
    lispic.splice(e.currentTarget.dataset.index, 1);
    lispic_path.splice(e.currentTarget.dataset.index, 1);
    this.setData({
      lispic: lispic,
      lispic_path: lispic_path,
      imgshow: false
    });
  }

});